package JSONoption;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.Data;
import org.junit.Before;
import org.junit.Test;

import java.util.*;

/**
 *
 * https://github.com/alibaba/fastjson/wiki/Quick-Start-CN
 *
 * JSON最佳实践
 * http://kimmking.github.io/2017/06/06/json-best-practice/
 *SerializerFeature属性
 * 名称	含义	备注
 * QuoteFieldNames	输出key时是否使用双引号,默认为true
 * UseSingleQuotes	使用单引号而不是双引号,默认为false
 * WriteMapNullValue	是否输出值为null的字段,默认为false
 * WriteEnumUsingToString	Enum输出name()或者original,默认为false
 * UseISO8601DateFormat	Date使用ISO8601格式输出，默认为false
 * WriteNullListAsEmpty	List字段如果为null,输出为[],而非null
 * WriteNullStringAsEmpty	字符类型字段如果为null,输出为”“,而非null
 * WriteNullNumberAsZero	数值字段如果为null,输出为0,而非null
 * WriteNullBooleanAsFalse	Boolean字段如果为null,输出为false,而非null
 * SkipTransientField	如果是true，类中的Get方法对应的Field是transient，序列化时将会被忽略。默认为true
 * SortField	按字段名称排序后输出。默认为false
 * WriteTabAsSpecial	把\t做转义输出，默认为false	不推荐
 * PrettyFormat	结果是否格式化,默认为false
 * WriteClassName	序列化时写入类型信息，默认为false。反序列化是需用到
 * DisableCircularReferenceDetect	消除对同一对象循环引用的问题，默认为false
 * WriteSlashAsSpecial	对斜杠’/’进行转义
 * BrowserCompatible	将中文都会序列化为\格式，字节数会多一些，但是能兼容IE 6，默认为false
 * WriteDateUseDateFormat	全局修改日期格式,默认为false。JSON.DEFFAULT_DATE_FORMAT = “yyyy-MM-dd”;JSON.toJSONString(obj, SerializerFeature.WriteDateUseDateFormat);
 * DisableCheckSpecialChar	一个对象的字符串属性中如果有特殊字符如双引号，将会在转成json时带有反斜杠转移符。如果不需要转义，可以使用这个属性。默认为false
 * NotWriteRootClassName	含义
 * BeanToArray	将对象转为array输出
 * WriteNonStringKeyAsString	含义
 * NotWriteDefaultValue	含义
 * BrowserSecure	含义
 * IgnoreNonFieldGetter	含义
 * WriteEnumUsingName	含义
 */
public class FastjonTest {

    private static Word word;



    /**
     * 1：
     * 字符串转对象  */
    @Test
    public void parseObject(){

        String wordStr="{\"a\":\"a\",\"b\":2,\"c\":true,\"d\":\"d\",\"date\":1621138254962,\"e\":\"\",\"map\":{\"user1\":{\"add\":\"北京\",\"id\":1,\"name\":\"用户1\",\"old\":\"11\"},\"user3\":{\"add\":\"广州\",\"id\":3,\"old\":\"33\"}}}";
        Word word = JSON.parseObject(wordStr, Word.class);

        System.out.println(JSONObject.toJSONString(word));

    }



    /**
     * 1：
     * UseSingleQuotes:使用单引号而不是双引号,默认为false  */
    @Test
    public void useSingleQuotes(){
        System.out.println(JSONObject.toJSONString(word));
        System.out.println("设置useSingleQuotes后：");
        System.out.println(JSONObject.toJSONString(word, SerializerFeature.UseSingleQuotes));
    }


    /**
     * 2:
     * WriteMapNullValue:是否输出值为null的字段,默认为false
     * map中的对象如果为object则会输出对象的hashcode，加入泛型信息后能正确序列化
     */
    @Test
    public  void writeMapNullValue() {
        System.out.println(JSONObject.toJSONString(word));
        System.out.println("设置WriteMapNullValue后：");
        System.out.println(JSONObject.toJSONString(word, SerializerFeature.WriteMapNullValue));
    }
    /**
     * 3:
     * UseISO8601DateFormat:Date使用ISO8601格式输出，默认为false
     */
    @Test
    public  void UseISO8601DateFormat() {
        System.out.println(JSONObject.toJSONString(word));
        System.out.println("设置UseISO8601DateFormat后：");
        //System.out.println(JSONObject.toJSONString(word, SerializerFeature.UseISO8601DateFormat));
        JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd";
        System.out.println(JSONObject.toJSONString(word, SerializerFeature.WriteDateUseDateFormat));
     }

    /**
     * 4:
     * WriteNullListAsEmpty:List字段如果为null,输出为[],而非null
     * 需要配合WriteMapNullValue使用，现将null输出
     */
    @Test
    public  void writeNullListAsEmpty() {
        word.setList(null);
        System.out.println(JSONObject.toJSONString(word));
        System.out.println("设置WriteNullListAsEmpty后：");
        System.out.println(JSONObject.toJSONString(word, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullListAsEmpty));
      }



    @Before
    public void init(){
        word = new Word();
        word.setA("a");
        word.setB(2);
        word.setC(true);
        word.setD("d");
        word.setE("");
        word.setF(null);
        word.setDate(new Date());

        List<User> list = new ArrayList<User>();
        User user1 = new User();
        user1.setId(1);
        user1.setOld("11");
        user1.setName("用户1");
        user1.setAdd("北京");
        User user2 = new User();
        user2.setId(2);
        user2.setOld("22");
        user2.setName("用户2");
        user2.setAdd("上海");
        User user3 = new User();
        user3.setId(3);
        user3.setOld("33");
        user3.setName(null);
        user3.setAdd("广州");

        list.add(user3);
        list.add(user2);
        list.add(null);
        list.add(user1);

        word.setList(list);

        Map<String , User> map = new HashMap<>();
        map.put("user1", user1);
        map.put("user3", user3);

        word.setMap(map);
    }


}

@Data
 class Word {

    private String d;
    private String e;
    private String f;
    private String a;
    private int b;
    private boolean c;
    private Date date;
    private Map<String , User> map;
    private List<User> list;
}
@Data
 class User {

    private int id;
    private String name;
    private String add;
    private String old;
}